********************************************************************************
** 	TITLE:		ON07_results                                                  ** 	
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ON07_results                                                  **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* 1. Download the "Valid Votes Cast for Each Candidate" file from this link: https://results.elections.on.ca/api/report-groups/4/report-outputs/510/csv  
* 2. Save the file to the CSV format with the following name: "ON07_results.csv"

* Import the dataset

import delimited "ON07_results.csv"

* Keep only general election

drop if isgeneralelection == 0

* Rename and (re)create variables

rename electoraldistrictnumber districtnumber07
rename electoraldistrictnameenglish districtname07
rename politicalinterestcode partyname07
rename totalvalidballotscast numberofvotes07
gen numberofvotespercent07 = percentoftotalvalidballotscast*100

* Generate dichotomous variable for each party

gen lib07 = 1 if partyname07 == "L"
replace lib07 = 0 if lib07!=1

gen con07 = 1 if partyname07 == "PC"
replace con07 = 0 if con07!=1

gen ndp07 = 1 if partyname07 == "ND"
replace ndp07 = 0 if ndp07!=1

gen green07 = 1 if partyname07 == "GP"
replace green07 = 0 if green07!=1

* Generate party variable

gen party07 = 1 if lib07 == 1
replace party07 = 2 if con07 == 1
replace party07 = 3 if ndp07 == 1
replace party07 = 4 if green07 == 1
replace party07 = 88 if party07 == . 

* Generate rank of parties

gsort districtname07 -numberofvotes07
bysort districtname07 : gen rank07 = _n

gen first07 = party07 if rank07 == 1
gen second07 = party07 if rank07 == 2
gen third07 = party07 if rank07 == 3

bysort districtname07 : gen winner07 = first07[1]
bysort districtname07 : gen runnerup07 = second07[2]
bysort districtname07 : gen thirdplace07 = third07[3]

* Generate margins of victory

bysort districtname07 : gen margin07 = numberofvotespercent07[1] - numberofvotespercent07[2]

* Generate effective number of electoral parties

gen sqrvotepr07 = (numberofvotespercent07/100)^2

bysort districtname07 : egen sumsqrvotepr07 = sum(sqrvotepr07)

gen enep07 = 1/sumsqrvotepr07

* Labelling parties

label define party 1 "Liberal Party" 2 "Progressive Conservative Party" 3 "New Democratic Party" 4 "Green Party" 88 "Other"
label values winner07 runnerup07 thirdplace07 party

* Selecting, ordering, and sorting variables

keep districtnumber07 districtname07 party07 rank07 numberofvotes07 numberofvotespercent07 margin07 enep07 winner07 runnerup07 thirdplace07

order districtnumber07 districtname07 party07 rank07 numberofvotes07 numberofvotespercent07 margin07 enep07 winner07 runnerup07 thirdplace07

sort districtnumber07 rank07

drop if rank07 > 3

* Drop duplicates

duplicates tag districtnumber07, gen(dup)
duplicates drop districtnumber07, force
drop dup

* Create election variable

gen election07 = "2007 Ontario general election"

* Saving file

save ON07_results, replace